import pygame
import sys
import random
# 初始化pygame
pygame.init()

# 设置屏幕大小
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))

# 加载背景图片# 设置窗口
background_image = pygame.image.load(r'C:\Users\lenovo\OneDrive\Pictures\Camera Roll\贪吃蛇背景3.jpg')

# 加载蛇头图片
snake_head_image = pygame.image.load(r'C:\Users\lenovo\OneDrive\Pictures\Camera Roll\微信图片_20240621084434.jpg')
#加载蛇身图片
# snake_tail_image= pygame.image.load()
# 设置游戏速度
clock = pygame.time.Clock()
speed = 5 # 设置帧率为10

# 定义蛇和食物的大小
block_size = 20

# 定义颜色
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PINK=(255,192,203)
# 蛇的初始位置
snake_pos = [[100, 100], [80, 100], [60, 100]]

# 食物的初始位置
food_pos = [300, 300]

def draw_snake(snake_pos):
    for i, pos in enumerate(snake_pos):
        if i == 0:
            screen.blit(snake_head_image, (pos[0], pos[1]))  # 使用蛇头图片
        else:
            pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], block_size, block_size))
# 定义一个函数来绘制蛇，参数是蛇的位置列表
def draw_snake(snake_pos):
    for pos in snake_pos:
        pygame.draw.circle(screen, GREEN, pos, 10)
    pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], 10, 10))

def draw_food(food_pos):
    pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], block_size, block_size))

def move_snake(snake_pos, direction):
    x, y = snake_pos[0]
    if direction == 'right':
        x += block_size
    elif direction == 'left':
        x -= block_size
    elif direction == 'up':
        y -= block_size
    elif direction == 'down':
        y += block_size
    snake_pos.insert(0, [x, y])
    snake_pos.pop()

def check_collision(snake_pos, food_pos):
    if snake_pos[0] == food_pos:
        return True
    return False

def generate_food(snake_pos):
    while True:
        x = random.randrange(0, screen_width, block_size)
        y = random.randrange(0, screen_height, block_size)
        if [x, y] not in snake_pos:
            return [x, y]

def check_game_over(snake_pos):
    x, y = snake_pos[0]
    if x < 0 or x >= screen_width or y < 0 or y >= screen_height or snake_pos[0] in snake_pos[1:]:
        return True
    return False

direction = 'right'

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and direction != 'down':
                direction = 'up'
            elif event.key == pygame.K_DOWN and direction != 'up':
                direction = 'down'
            elif event.key == pygame.K_LEFT and direction != 'right':
                direction = 'left'
            elif event.key == pygame.K_RIGHT and direction != 'left':
                direction = 'right'

    move_snake(snake_pos, direction)

    if check_collision(snake_pos, food_pos):
        food_pos = generate_food(snake_pos)
        snake_pos.append(snake_pos[-1])

    if check_game_over(snake_pos):
        break

    screen.blit(background_image, (0, 0))  # 绘制背景图片
    draw_snake(snake_pos)
    draw_food(food_pos)
    pygame.display.flip()
    clock.tick(speed)  # 设置帧率为10

pygame.quit()